//
//  SHOrdersNotificationDetailView.swift
//  ServiceHelper
//
//  Created by Xuedan on 15/11/2.
//  Copyright © 2015年 Stan Wu. All rights reserved.
//

import UIKit
import AVFoundation

protocol SHOrderNotificationDetailDelegate{
    func orderNotificationUpdated(order: NSDictionary?)
    func showSelectedView(dic: NSDictionary?)
}

class SHOrdersNotificationDetailView: UIView{
    
    var vDetail:UIView!
    var vLine : UIView!
    var strNew :NSString!
    var strOrderStatus :NSString!
    var lblOrderNumber :UILabel!
    var lblOrderStatus :UILabel!
    var lblOrderNumberBig :UILabel!
    var lblCarNumber :UILabel!
    var lblTitle : UILabel!
    var lblNotificationTime :UILabel!
    var btnShowDetail:UIButton!
    var btnSelected:UIButton!
    var btnReset:UIButton!
    var checkNO = NSString()
    var carNO = NSString()
    var dicOrderDetail = NSDictionary()

    
    var strStatus: NSString?{
        didSet{
            strOrderStatus = strStatus!
            
            
            if strOrderStatus == "2"{
                lblNotificationTime.hidden = true
                lblOrderNumber.hidden = true
                btnShowDetail.hidden = true
                
                lblOrderStatus.hidden = false
                
//                self.changeLineFrame()

            }
        }
    }
    
//    var str: NSString?{
//        didSet{
//            strNew = str!
//            switch strNew{
//            case "全部订单":
//                btnSelected.setTitle("选择取车员", forState: .Normal)
//                btnReset.setTitle("取消", forState: .Normal)
//            case "等待确认":
//                btnSelected.setTitle("选择取车员", forState: .Normal)
//                btnReset.setTitle("取消", forState: .Normal)
//            default:
//                break
//                
//            }
//        }
//    }
    
    var orderNotificationInfo: NSDictionary?{
        didSet{
            if orderNotificationInfo != nil{
                vDetail.hidden = false
            }else{
                vDetail.hidden = true
            }
            
            if orderNotificationInfo != nil{
                let order_type = orderNotificationInfo?.objectForKey("order_info")?.objectForKey("order_type") as! String
                if order_type == "1"{
                    lblTitle.text = "预约保养服务";
                }else if order_type == "2"{
                    lblTitle.text = "预约维修服务";
                }else if order_type == "2"{
                    lblTitle.text = "预约维修和保养服务";
                }
                
                checkNO = orderNotificationInfo?.objectForKey("order_info")?.objectForKey("check_no") as! String!
                btnShowDetail.setTitle("\(checkNO)  >>", forState: UIControlState.Normal)
                lblOrderNumberBig.text = "预约单号：\(checkNO)"
                
                if  orderNotificationInfo?.objectForKey("order_info")!.objectForKey("car_info")!.objectForKey("plate_no") != nil{
                    carNO = orderNotificationInfo?.objectForKey("order_info")!.objectForKey("car_info")!.objectForKey("plate_no")! as! String
                    lblCarNumber.text = "车牌号码：\(carNO)"
//                    lblCarNumber.text = "车牌号码：\(carNO)"
                }else{
                    carNO = orderNotificationInfo?.objectForKey("order_info")!.objectForKey("car_info")!.objectForKey("vin")! as! String!
                    lblCarNumber.text = "VIN：\(carNO)"
                }
                
                

            }
            
            
//            if checkNO == orderNotificationInfo?.objectForKey("check_no") as! String!{
//                btnShowDetail.setTitle("\(checkNO)  >>", forState: UIControlState.Normal)
//                 lblOrderNumberBig.text = "预约单号：\(checkNO)"
//                
//            }
            

        }
//        refreshData()
    }
    
    var delegateNotification: SHOrderNotificationDetailDelegate?
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
    // Drawing code
    }
    */
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        vDetail = UIView(frame: CGRectMake(0, 0,frame.size.width,frame.size.height))
        vDetail.backgroundColor = UIColor.whiteColor()
        vDetail.layer.shadowColor = UIColor.blackColor().CGColor
        vDetail.layer.shadowRadius = 1
        vDetail.layer.shadowOffset = CGSizeMake(-2, 0)
        vDetail.layer.shadowOpacity = 0.1
        
        
        lblTitle = UILabel.create(CGRectMake(29, 25, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
//        lblTitle.text = "预约维修和保养服务"
        vDetail.addSubview(lblTitle)
        
        
        var btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(530, 20, 40, 40)
        btn.setImage(UIImage(named: "left"), forState: .Normal)
        btn.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        vDetail.addSubview(btn)
        btn.tag = 100
        btn.hidden = true
        
        
        btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(570, 20, 40, 40)
        btn.setImage(UIImage(named: "right"), forState: .Normal)
        btn.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        vDetail.addSubview(btn)
        btn.tag = 101
        btn.hidden = true
        
        
        btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(610, 20, 40, 40)
        btn.setImage(UIImage(named: "other"), forState: .Normal)
        btn.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        vDetail.addSubview(btn)
        btn.tag = 102
        btn.hidden = true
        
        
        lblOrderNumber = UILabel.create(CGRectMake(29, 63, 120, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
        lblOrderNumber.text = "预约单号："
        vDetail.addSubview(lblOrderNumber)
        
        
       
        
        btnShowDetail = UIButton(type: .Custom)
        btnShowDetail.frame = CGRectMake(160, 63, 200, 25)
        
        if let checkNo = orderNotificationInfo?.objectForKey("check_no") as! String!{
            btnShowDetail.setTitle("\(checkNo)  >>", forState: UIControlState.Normal)

        }else{
            btnShowDetail.setTitle("  >>", forState: UIControlState.Normal)

        }
        btnShowDetail.setTitleColor(UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1), forState: UIControlState.Normal)
        btnShowDetail.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        vDetail.addSubview(btnShowDetail)
        btnShowDetail.tag = 103
        
        lblNotificationTime = UILabel.create(CGRectMake(29, 96, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
//        lblNotificationTime.text = "2015-10-08（星期一） 16：32"
        vDetail.addSubview(lblNotificationTime)
        
        lblOrderStatus = UILabel.create(CGRectMake(29, 70, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1))
//        lblOrderStatus.text = "支付完成，待上门取车!"
        vDetail.addSubview(lblOrderStatus)
        lblOrderStatus.hidden = true
        
        vLine = UIView (frame: CGRectMake(0 ,140,frame.size.width,1))
        vLine.backgroundColor = UIColor(white: 0.90, alpha: 1)
        vDetail.addSubview(vLine)

        lblOrderNumberBig = UILabel.create(CGRectMake(29, 165, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
        vDetail.addSubview(lblOrderNumberBig)
        
        
        lblCarNumber = UILabel.create(CGRectMake(29, 215, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
//        lblCarNumber.text = "车牌号码：沪A-GX588"
        vDetail.addSubview(lblCarNumber)
        
        
        btnSelected = UIButton(type: .Custom)
        btnSelected.frame = CGRectMake(70, 640, 250, 44)
        btnSelected.setBackgroundImage(UIImage.colorImage(UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1), size: CGSizeMake(1, 1)), forState: .Normal)
        btnSelected.setTitle("预约确认", forState: .Normal)
        btnSelected.titleLabel?.font = UIFont.systemFontOfSize(18)
        btnSelected.setTitleColor(UIColor.whiteColor(), forState:.Normal)
        btnSelected.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        btnSelected.clipsToBounds = true
        btnSelected.layer.cornerRadius = 4
        btnSelected.layer.borderWidth = 0.5
        btnSelected.layer.borderColor = UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1).CGColor
        vDetail.addSubview(btnSelected)
        btnSelected.tag = 1000
        btnSelected.hidden = true
        
        btnReset = UIButton(type: .Custom)
        btnReset.frame = CGRectMake(385, 640, 250, 44)
        btnReset.setBackgroundImage(UIImage.colorImage(UIColor(red: 0.95, green: 0.96, blue: 0.97, alpha: 1), size: CGSizeMake(1, 1)), forState: .Normal)
        btnReset.setTitle("预约变更", forState: .Normal)
        btnReset.titleLabel?.font = UIFont.systemFontOfSize(18)
        btnReset.setTitleColor(UIColor(white: 0.53, alpha: 1), forState:.Normal)
        btnReset.addTarget(self, action: #selector(SHOrdersNotificationDetailView.confirmClicked(_:)), forControlEvents: .TouchUpInside)
        btnReset.clipsToBounds = true
        btnReset.layer.cornerRadius = 4
        btnReset.layer.borderWidth = 0.5
        btnReset.layer.borderColor = UIColor(white: 0.53, alpha: 1).CGColor
        vDetail.addSubview(btnReset)
        btnReset.tag = 1002
        btnReset.hidden = true
        
        self.addSubview(vDetail)
        
        
        self.refreshData()
    }
    
    func refreshData(){
        delegateNotification?.orderNotificationUpdated(orderNotificationInfo)
    }
    
    func changeLineFrame(){
        var frame = vLine.frame
        frame.origin.y = 120
        vLine.frame = frame
        print(vLine.frame)
    }
    
    
    func confirmClicked(sender:UIButton){
        switch sender.tag{
        case 100:
            print("100")
        case 101:
            print("101")
        case 102:
            print("102")
        case 103:
            print("103")
            self.getNewOrderDetail()
            
        case 1000:
            print("1000-预约确认")
        case 1002:
            print("1002-预约变更")
            
            default:()
            
        }
        
        
    }
    
    //获取最新订单详情
    func getNewOrderDetail(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.orderNotificationInfo?.objectForKey("order_id") as! String!{
                    let dict = SHDataProvider.getOrderDetail(NSDictionary(object: order_id,forKey: "order_id"))
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                self.dicOrderDetail = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                print(self.dicOrderDetail)
                                self.delegateNotification?.showSelectedView(self.dicOrderDetail)

//                                self.updateAllOrdersInfo()
                                
                            }
                        }
                    })
                }
            })
        }
        
    }

    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
